<?php
include 'lanewechat.php';
include 'WeiXinPay.php';
include 'login.php';
ini_set( 'error_reporting' , 'E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING' );
if( !empty( $_POST ) ){
    $result = getSession( $_POST['openid'] );
    //挂失
    $url = 'http://localhost:8080/MallInterface/QueryVipBalanceInfo.aspx';
    $url .= '?Type=4&sid='.$result['sessionId'].'&VipId='.$result['VipId'];
    $info = file_get_contents( $url );
    $info = json_decode( $info , true );
    exit(
        json_encode(
            array(
                'error' => 0,
                'data' => array(
                    $result,
                    $url
                )
            )
        )
    );
}
$openid = (string)$_SESSION['openid'];
$info = array();
if( empty( $openid ) ){
    $code = $_GET['code'];
    if( empty( $code ) ){
        header( 'Location: info.php?backUrl=query.php' );exit;
    }
    $result = \LaneWeChat\Core\WeChatOAuth::getAccessTokenAndOpenId($code);
    $info = \LaneWeChat\Core\UserManage::getUserInfo( $result['openid'] );
    $openid = $_SESSION['openid'] = $info['openid'];
}
$result = getSession( $openid );
if( empty( $result ) ){
    exit('<script>alert("暂未开卡～，请先前去开卡");window.location.href="opencard.php"</script>');
}
$url = 'http://localhost:8080/MallInterface/QueryVipBalanceInfo.aspx';
$url .= '?Type=2&sid='.$result['sessionId'].'&VipId='.$result['VipId'];

$info = file_get_contents( $url );
$info = json_decode( $info , true );
Mysql::getInstance()->insert(
    'lw_event_log',
    'createdate,url',
    time() . ',"'.$url.'"'
);

if( $info['status'] != 0 ){
    exit('<script>alert("卡已挂失，请重新开卡~");window.location.href="opencard.php"</script>');
}
?>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/> 
    <title>查询信息</title>
    <style type="text/css">
        ul {
            margin-left:10px;
            margin-right:10px;
            margin-top:10px;
            padding: 0;
        }
        li {
            width: 32%;
            float: left;
            margin: 0px;
            margin-left:1%;
            padding: 0px;
            height: 100px;
            display: inline;
            line-height: 100px;
            color: #fff;
            font-size: x-large;
            word-break:break-all;
            word-wrap : break-word;
            margin-bottom: 5px;
        }
        a {
            -webkit-tap-highlight-color: rgba(0,0,0,0);
        	text-decoration:none;
            color:#fff;
        }
        a:link{
            -webkit-tap-highlight-color: rgba(0,0,0,0);
        	text-decoration:none;
            color:#fff;
        }
        a:visited{
            -webkit-tap-highlight-color: rgba(0,0,0,0);
        	text-decoration:none;
            color:#fff;
        }
        a:hover{
            -webkit-tap-highlight-color: rgba(0,0,0,0);
        	text-decoration:none;
            color:#fff;
        }
        a:active{
            -webkit-tap-highlight-color: rgba(0,0,0,0);
        	text-decoration:none;
            color:#fff;
        }
    </style>
    <script src="js/jquery.js" ></script>
</head>
<body>
	<div align="center">
        <h3>余额：¥ <?=$info['data']['SumCash'] / 100;?> 元</h3>
	</div>
	<div align="center">
        <ul>
            <li style="background-color:#FF7F24"><a href="recharge.php">在线充值</a></li>
            <li style="background-color:#698B22"><a href="#" onclick="lossCard()">我要挂失</a></li>
<!--            <li style="background-color:#8B6914"><a href="http://paysdk.weixin.qq.com/example/native.php">扫码支付</a></li>-->
<!--            <li style="background-color:#CDCD00"><a href="http://paysdk.weixin.qq.com/example/orderquery.php">订单查询</a></li>-->
<!--            <li style="background-color:#CD3278"><a href="http://paysdk.weixin.qq.com/example/refund.php">订单退款</a></li>-->
<!--            <li style="background-color:#848484"><a href="http://paysdk.weixin.qq.com/example/refundquery.php">退款查询</a></li>-->
<!--            <li style="background-color:#8EE5EE"><a href="http://paysdk.weixin.qq.com/example/download.php">下载订单</a></li>-->
        </ul>
	</div>
<script>
    var openid = '<?=$openid?>';
    function lossCard(){
        if( !confirm('确认要挂失吗？') ){
            return false;
        }
        $.ajax({
            url:'query.php',
            type:'post',
            data:{
                openid:openid
            },
            dataType:'json'
        }).done(function(data){
            alert('挂失成功!'+JSON.stringify( data.data ) );
            //alert('挂失成功!');
            window.location.reload();
        });
    }
</script>
</body>
</html>